package com.mc.common.guava.io;

import com.google.common.base.Joiner;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import org.junit.After;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;

/**
 * @author cc
 * @ClassName FileTest.java
 * @Description guava文件类测试
 * @createTime 2019年07月24日 16:10:00
 */
public class FileTest {
    private final String source_file = "D:\\myspace\\mcgo\\mcgo-common\\src\\main\\resources\\io\\source.txt";
    private final String target_file = "D:\\myspace\\mcgo\\mcgo-common\\src\\main\\resources\\io\\target.txt";

    @Test
    public void testFileCopyWithGuava() throws IOException {
        File targetFile = new File(target_file);
        Files.copy(new File(source_file), targetFile);
        assertThat(targetFile.exists(), equalTo(true));

    }

    //测试完毕后删除目标文件
    @After
    public void deleteTargetFile() {
        File file = new File(target_file);
        if (file.exists()) {
            file.delete();
        }
    }

    @Test
    public void testMoveFile() throws IOException {
        try {
            Files.move(new File(source_file), new File(target_file));
            assertThat(new File(target_file).exists(), equalTo(true));
            assertThat(new File(source_file).exists(), equalTo(false));
        } finally {
            Files.move(new File(target_file), new File(source_file));
        }
    }

    @Test
    public void testJoinString() throws IOException {
        String str = "C:\\Program Files\\Java\\jdk1.8.0\\bin\\java.exe\" -Dmaven.multiModuleProjectDirectory=D:\\myspace\\mcgo\\mcgo-common -Dmaven.home=\n" +
                "D:\\java\\apache-maven-3.6.0 -Dclassworlds.conf=D:\\java\\apache-maven-3.6.0\\bin\\m2.conf \"\n";
        List<String> strings = Files.readLines(new File(source_file), Charset.defaultCharset());
        String join = Joiner.on("\n").join(strings);
        assertThat(join, equalTo(str));

    }

    @Test
    public void testRecursive() {
        ArrayList<File> list = Lists.newArrayList();
        String path = "C:\\Users\\Administrator\\Desktop\\新建文件夹\\galaxy-log";
        recursiveList(new File(path), list);
        list.forEach(System.out::println);
    }

    private void recursiveList(File root, List<File> list) {
        if (root.isHidden()) return;
        if (root.isFile()) {
            list.add(root);
            root.deleteOnExit();
        } else {
            File[] files = root.listFiles();
            for (File file : files) {
                recursiveList(file, list);
            }
            root.delete();
        }
    }

    @Test
    public void testTreeList() {
        File file = new File("D:\\yimididaspace\\galaxy-mobile");
        Iterable<File> files = Files.fileTraverser().depthFirstPreOrder(file);
        files.forEach(x -> {
            if (x.isFile()) {
                System.out.println(x);
            }
        });
        System.out.println("============================================================");
        Iterable<File> filesPost = Files.fileTraverser().depthFirstPostOrder(file);
        filesPost.forEach(x -> {
            if (x.isFile()) {
                System.out.println(x);
            }
        });
    }
}
